package com.rxyb.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 交换机类型:headers(根据消息内容中的headers属性进行匹配)
 *
 * @author lcf
 * @date 2020-05-25-14:54
 */
@Configuration
public class HeadersExchangeConfig {

    /**
     * 注入Headers路由策略的Exchange交换机实例
     *
     * @return Exchange “交换机”实例
     */
    @Bean(name = "myHeadersExchange")
    HeadersExchange getDirectExchange() {
        // 创建并返回名为My-Headers-Exchange的交换机
        return new HeadersExchange("My-Headers-Exchange");
    }

    /**
     * 将Queue绑定到此headersExchange(并指定:当headers中所有的“map”被此Queue匹配时,才可使用此队列)
     * 注:此示例是匹配的.whereAll(Map<String,Object> map);
     *    也可以只匹配.whereAll(String... headersKeys);
     *
     * @return Binding
     */
    @Bean
    Binding bindingQueueOneToHeadersAllExchange(@Qualifier("myFirstQueue") Queue myFirstQueue,
                                                @Qualifier("myHeadersExchange") HeadersExchange myHeadersExchange) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("name", "邓沙利文");
        headers.put("motto", "justry");
        return BindingBuilder.bind(myFirstQueue).to(myHeadersExchange).whereAll(headers).match();
    }

    /**
     * 将Queue绑定到此headersExchange(并指定:当headers中任意一个map被此Queue匹配时,就会使用此队列)
     * 注:此示例是匹配的.whereAny(Map<String,Object> map);
     *    也可以只匹配.whereAny(String... headersKeys);
     *
     * @return Binding
     */
    @Bean
    Binding bindingQueueOneToHeadersAnyExchange(@Qualifier("myThreeQueue") Queue myThreeQueue,
                                                @Qualifier("myHeadersExchange") HeadersExchange myHeadersExchange) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("name", "邓沙利文");
        headers.put("motto", "justry");
        return BindingBuilder.bind(myThreeQueue).to(myHeadersExchange).whereAny(headers).match();
    }


}
